package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.vo.MarketCategoryVo;
import com.cskaoyan.market.vo.MarketPrimaryCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MarketCategoryServiceImpl implements MarketCategoryService {

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Override
    public Map<String, Object> list() {
        // 一次性查询表
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLogicalDeleted(true);
        List<MarketCategory> categories = categoryMapper.selectByExample(categoryExample);
        long count = categoryMapper.countByExample(categoryExample);

        // 返回响应需要的内容
        Map<String, Object> result = new HashMap<>();
        // 填充
        List<MarketCategoryVo> categoryList = new ArrayList<>();
        // 根据pid对商品类目信息分组
        Map<Integer, List<MarketCategory>> categoriesByPid = groupByPid(categories);

        // 先获取一级类目
        List<MarketCategory> l1Categories = categoriesByPid.get(0);
        for (MarketCategory l1 : l1Categories) {
            // 获取一级类目下的二级类目
            List<MarketCategoryVo> childrenOfl1 = new ArrayList<>();

            MarketCategoryVo l1Vo = new MarketCategoryVo(l1.getId(), l1.getName(), l1.getKeywords(),
                    l1.getDesc(), l1.getIconUrl(), l1.getPicUrl(), l1.getLevel(), childrenOfl1);
            categoryList.add(l1Vo);
            List<MarketCategory> l2Categories = categoriesByPid.get(l1.getId());
            // 判断二级类目不为空
            if (l2Categories != null && !l2Categories.isEmpty()) {
                for (MarketCategory l2 : l2Categories) {
                    childrenOfl1.add(new MarketCategoryVo(l2.getId(), l2.getName(), l2.getKeywords(),
                            l2.getDesc(), l2.getIconUrl(), l2.getPicUrl(), l2.getLevel(), null));
                }
            }
        }

        result.put("total", count);
        result.put("pages", 1);
        result.put("limit", count);
        result.put("page", 1);
        result.put("list", categoryList);
        return result;
    }

    @Override
    public Map<String, Object> primaryCategoryList() {
        // 一次性查询表
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        // 一次性查询表
        List<MarketCategory> categories = categoryMapper.selectByExample(categoryExample);
        long count = categoryMapper.countByExample(categoryExample);

        // 返回响应需要的内容
        Map<String, Object> result = new HashMap<>();
        // 填充
        List<MarketPrimaryCategoryVo> primaryCategoryList = new ArrayList<>();

        for (MarketCategory category : categories) {
            primaryCategoryList.add(new MarketPrimaryCategoryVo(category.getId(), category.getName()));
        }

        result.put("total", count);
        result.put("pages", 1);
        result.put("limit", count);
        result.put("page", 1);
        result.put("list", primaryCategoryList);
        return result;
    }

    @Override
    public void create(MarketCategory marketCategory) {
        categoryMapper.insert(marketCategory);
    }

    @Override
    public void update(MarketCategory marketCategory) {
        categoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public void delete(Integer id) {
        categoryMapper.logicalDeleteByPrimaryKey(id);
    }

    private Map<Integer, List<MarketCategory>> groupByPid(List<MarketCategory> categories) {
        Map<Integer, List<MarketCategory>> categoriesByPid = new HashMap<>();
        for (MarketCategory category : categories) {
            Integer pid = category.getPid();
            List<MarketCategory> list = categoriesByPid.getOrDefault(pid, new ArrayList<>());
            list.add(category);
            categoriesByPid.put(pid, list);
        }
        return categoriesByPid;
    }
}
